#!python
#从SwitchHosts!导入配置文件
#蒋晓岗<446252221@qq.com>
import io
import os
import json

#创建子目录etc
def safe_mkdir(name):
	try:
		os.mkdir(name)
	except:
		return None

#保存配置
def save_hosts(dir, name, data):
	print("正在导入:", name)
	hosts = open(dir + "\\" + name + ".hosts", "w")
	hosts.write(data)
	hosts.close()

#入口
home = os.environ["USERPROFILE"]
file_name = home + "\\.SwitchHosts\\data.json"
file_obj = open(file_name, "r", encoding="utf8")
json_str = file_obj.read()
file_obj.close()
list_obj = json.loads(json_str)
for data in list_obj["list"]:
	title = data["title"]
	content = data["content"]
	safe_mkdir("etc")
	save_hosts("etc", title, content)
	